package cn.hh;

import cn.hh.bean.reqBean.ParamBean;
import cn.hh.utils.Bean2Utils;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.io.FileUtils;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.util.Map;

/**
 * @Author huangh
 * @date 2021/08/04
 * @Description:
 */
public class MapToBean {

    /**
     * hutool
     * BeanUtil.beanToMap(paramBean) 只能转一层 多层map里面还是bean
     *
     * commons-beanutils
     * BeanUtils.describe(param);  返回string
     *
     * Bean2Utils.objectToMap 自己找的可以转换多层
     *
     * @throws IOException
     */
    @Test
    public void mapToBean1() throws Exception {
        //读取文件内容转为bean
        String basePath2 = System.getProperty("user.dir");
        File file = new File(basePath2+"/file/custInfoParam.json");
        String content = FileUtils.readFileToString(file,"UTF-8");
        ParamBean paramBean = JSONUtil.toBean(content, ParamBean.class);

        Map<String,Object> map = BeanUtil.beanToMap(paramBean);
        System.out.println("hutool方法转 head为" + map.get("head").getClass());

        map = BeanUtils.describe(paramBean);
        System.out.println("commons-beanutils方法转 head为" + map.get("head").getClass());

        map = Bean2Utils.objectToMap(paramBean);
        System.out.println("Bean2Utils方法转 head为" + map.get("head").getClass());

    }

}
